//
//  touzifangshiViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2019/1/10.
//  Copyright © 2019 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class touzifangshiViewController: UIViewController {
    var tableView  : UITableView!
    
    
    var shiID :String!
    
    var shiName:String!
    

    
    var shiArray = [touzifangCity]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = bgGrayColor;
        
        navigationItem.title="选择市"
        tableView = UITableView(frame:self.view.frame, style:.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        
        tableView.tableFooterView = UIView.init()
        
        
        self.view.addSubview(tableView)
        tableView.sd_layout()?.spaceToSuperView(UIEdgeInsets.zero)
        
        self.loadData()
    }
    
    func loadData(){
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/city/list"
        
       

        let json = "{\"provinceId\":\"\(self.shiID!)\"}"
        
        
        print("======\(json)")
        
    
       
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        Alamofire.request(request).responseJSON {(response) in
            
              print("======\(response)")
            
          
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {

                    let model1 =   touzifangCity()


                    model1.ids = dataDic["id"].intValue

                    model1.cityName = dataDic["cityName"].string


                    self.shiArray.append(model1)

                
                    

                }
                
                
                
                self.tableView.reloadData()
                
                
            }else{
                
            }
            
        }
        
        
        
    }

    
    
    
    
}
extension touzifangshiViewController: UITableViewDelegate, UITableViewDataSource {
    
    
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.shiArray.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 40
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
                let nextListModel:touzifangCity = self.shiArray[indexPath.row]
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            
            cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = nextListModel.cityName
        cell?.textLabel?.font = UIFont.systemFont(ofSize:20)
        
        return cell!
        
        
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
                let dynamicModel:touzifangCity = self.shiArray[indexPath.row]
        
        
                let WebVC =  touzifangxianViewController()
                WebVC.shiID = String(dynamicModel.ids)
        
                WebVC.shiName = dynamicModel.cityName
        
                WebVC.shenID = self.shiID
                WebVC.shenName = self.shiName
        
        
                self.navigationController?.pushViewController(WebVC , animated: true)
        
    }
    
    
}

